package com.example.task1.ui.user.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.BaseBean;
import com.example.task1.bean.UserInfo;
import com.example.task1.util.ApiUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserInfoActivity extends BaseActivity {

    private CardView cardView;
    private ImageView userImage;
    private EditText userName;
    private RadioGroup userSex;
    private EditText userPhone;
    private RadioButton userSexN;
    private RadioButton userSexV;
    private LinearLayout linearLayout;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();
        initData();
        btnSave.setOnClickListener(v -> {
            setData();
        });
    }

    @Override
    public String getMyTitle() {
        return "修改信息";
    }

    private void setData() {
        HashMap<String, Object> map = new HashMap<>();
        if (userSexV.isChecked()) {
            map.put("sex", "1");
        } else {
            map.put("sex", "0");
        }
        String name = userName.getText().toString();
        String phone = userPhone.getText().toString();
        if (TextUtils.isEmpty(name)) {
            userName.setError("用户名不能为空");
        } else if (phone.length() < 11) {
            userPhone.setError("请输入正确格式的电话号码");
        } else {
            map.put("phonenumber", phone);
            map.put("nickName", name);
        }
        Call<ResponseBody> put = MyApplication.getApiService().put(MyApplication.TOKEN, "/prod-api/api/common/user", ApiUtils.getBody(map));
        put.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    Gson gson = new Gson();
                    BaseBean bean = gson.fromJson(body, BaseBean.class);
                    if (bean.getCode() == 200) {
                        MyApplication.show("保存成功");
                        SharedPreferences.Editor edit = MyApplication.getSharedPreferences().edit();
                        edit.putString("username",name);
                        edit.commit();
                        finish();
                    } else {
                        MyApplication.show("保存失败，请稍后再试");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    private void initData() {
        Call<ResponseBody> apiRespCall = MyApplication.getApiService().get(MyApplication.TOKEN, "/prod-api/api/common/user/getInfo");
        apiRespCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    Gson gson = new Gson();
                    UserInfo userInfo = gson.fromJson(body, UserInfo.class);
                    UserInfo.UserDTO user = userInfo.getUser();
                    if (Objects.isNull(user.getNickName())) {
                        userName.setText("用户" + user.getPhonenumber());
                    } else {
                        userName.setText(user.getNickName() + "");
                    }
                    if (TextUtils.equals(user.getSex(), "1")) {
                        userSexV.setChecked(true);
                    }
                    userPhone.setText(user.getPhonenumber());
                    if (!user.getAvatar().isEmpty()) {
                        Glide.with(UserInfoActivity.this).load(user.getAvatar()).into(userImage);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    private void initView() {
        cardView = (CardView) findViewById(R.id.cardView);
        userImage = (ImageView) findViewById(R.id.user_image);
        userName = (EditText) findViewById(R.id.user_name);
        userSex = (RadioGroup) findViewById(R.id.user_sex);
        userPhone = (EditText) findViewById(R.id.user_phone);
        userSexN = (RadioButton) findViewById(R.id.user_sex_n);
        userSexV = (RadioButton) findViewById(R.id.user_sex_v);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        btnSave = (Button) findViewById(R.id.btn_save);
    }
}